{% extends "layout.html" %}
{% block content %}
    <div class="x-body layui-anim layui-anim-up">
        <form class="layui-form">
			<table class="layui-table">
				<tbody>
					<tr>
						<th>寄存器类型</th>
						<td colspan="3">
							<select id="register_type" name="register_type" lay-filter="register_type">
                                <option value="1">线圈状态</option>
                                <option value="2">输入状态</option>
                                <option value="3">保持寄存器</option>
                                <option value="4">输入寄存器</option>
                            </select>
						</td>
					</tr>
					<tr>
						<th>过滤器</th>
						<td>
							<input type="text" id="filter" name="filter" required="required" lay-verify="filter"
                            autocomplete="off" class="layui-input" value="">
						</td>
                        <td colspan="2">
                            使用 数字,数字:数字 形式，如:1,3,5:9
                        </td>
					</tr>
					<tr>
						<td colspan="2" style="text-align: center;">
                            <button id="confirmBtn" class="layui-btn" lay-filter="save" lay-submit="">
                                增加
                            </button>
                        </td>
						<td colspan="2" style="text-align: center;">
							<button  type="reset" class="layui-btn">
                                重置
                            </button>
						</td>
					</tr>
                </tbody>
            </table>
        </form>
    </div>
    <script>
        var channel_id=GetQueryString('channel_id');
        var channel_data_filter_id=GetQueryString('channel_data_filter_id');
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
            var form = layui.form
            ,layer = layui.layer;
            //自定义验证规则
            layer.ready(function(){
                if(channel_data_filter_id!=null){
                    data={'channel_data_filter_id':channel_data_filter_id};
                    $('#confirmBtn').html('保存');
                    $.ajax(
                    {
                        url:'readChannel_data_filter'
                        ,type:'POST'
                        ,dataType:'json'
                        ,data:data
                        ,success:function(data){
                            //设置内容
                            if(data.code==0){
                                $('#register_type').val(data.data[0].register_type);
                                $('#filter').val(data.data[0].filter);
                                layui.form.render();
                            }
                        }
                    });
                }
            });
            form.verify({
                filter: function(value){
                    var exp=/^((\d+|(\d+:\d+)),)*((\d+|(\d+:\d+))),*$/;
                    if(!exp.test(value)){
                        return '必须符合语法';
                    }
                },
            });
            //监听提交
            form.on('submit(save)', function(data){
                console.log(data.field);
                data.field['channel_id']=channel_id;
                if(channel_data_filter_id!=null)
                    data.field['channel_data_filter_id']=channel_data_filter_id;
                $.ajax({url:'saveChannel_data_filter',type:'POST',dataType:'json',data:data.field,success:function(data){
                    // console.log(data);
                    if(data.code==0){
                        // layer.msg('操作成功!');
                        layer.alert("操作成功", {icon: 6},function () {
                            parent.location.reload();
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    }
                }});
                return false;
            });
        });
    </script>
{% endblock %}